//
//  YKLoadingViewController.m
//  AutocCarApp
//
//  Created by dllo on 15/11/25.
//  Copyright © 2015年 尹凯. All rights reserved.
//

#import "YKLoadingViewController.h"
#import "YJViewController.h"

@interface YKLoadingViewController ()<UITextFieldDelegate>

@property (nonatomic, retain)UISegmentedControl *segmentC;

@property (nonatomic, retain)UIView *loginView;
@property (nonatomic, retain)UITextField *firstField;
@property (nonatomic, retain)UITextField *secondField;

@property (nonatomic, retain)UIView *registerView;
@property (nonatomic, retain)UITextField *register1;
@property (nonatomic, retain)UITextField *register2;
@property (nonatomic, retain)UITextField *register3;
@property (nonatomic, retain)UITextField *register4;

@end

@implementation YKLoadingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction:)];
    
    NSArray *array = [NSArray arrayWithObjects:@"登录", @"注册", nil];
    self.segmentC = [[UISegmentedControl alloc] initWithItems:array];
    [self.segmentC setTintColor:[UIColor blackColor]];
    self.segmentC.backgroundColor = [UIColor whiteColor];
    self.segmentC.frame = CGRectMake(125, 0, 125, 40);
    [self.navigationController.navigationBar addSubview:self.segmentC];
    self.segmentC.selectedSegmentIndex = 0;
    
    [self.segmentC addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    
    self.registerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Width, Height - 64)];
    self.registerView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    [self.view addSubview:self.registerView];
    
    UIView *thirdView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, Width, 50)];
    thirdView.backgroundColor = [UIColor whiteColor];
    [self.registerView addSubview:thirdView];
    
    self.register1 = [[UITextField alloc] initWithFrame:CGRectMake(25, 0, 335, 50)];
    self.register1.backgroundColor = [UIColor whiteColor];
    self.register1.placeholder = @"用户名";
    self.register1.font = [UIFont systemFontOfSize:20];
    self.register1.delegate = self;
    [thirdView addSubview:self.register1];
    
    UIView *forthView = [[UIView alloc] initWithFrame:CGRectMake(0, 70 , Width, 50)];
    forthView.backgroundColor = [UIColor whiteColor];
    [self.registerView addSubview:forthView];
    
    self.register2 = [[UITextField alloc] initWithFrame:CGRectMake(25, 0, 335, 50)];
    self.register2.backgroundColor = [UIColor whiteColor];
    self.register2.placeholder = @"密码";
    self.register2.font = [UIFont systemFontOfSize:20];
    self.register2.delegate = self;
    self.register2.secureTextEntry = YES;
    self.register2.clearButtonMode = UITextFieldViewModeAlways;
    [forthView addSubview:self.register2];
    
    UIView *fifthView = [[UIView alloc] initWithFrame:CGRectMake(0, 120, Width, 50)];
    fifthView.backgroundColor = [UIColor whiteColor];
    [self.registerView addSubview:fifthView];
    
    self.register3 = [[UITextField alloc] initWithFrame:CGRectMake(25, 0, 335, 50)];
    self.register3.backgroundColor = [UIColor whiteColor];
    self.register3.placeholder = @"确认密码";
    self.register3.font = [UIFont systemFontOfSize:20];
    self.register3.delegate = self;
    self.register3.secureTextEntry = YES;
    self.register3.clearButtonMode = UITextFieldViewModeAlways;
    [fifthView addSubview:self.register3];
    
    UIView *sixthView = [[UIView alloc] initWithFrame:CGRectMake(0, 170, Width, 50)];
    sixthView.backgroundColor = [UIColor whiteColor];
    [self.registerView addSubview:sixthView];
    
    self.register4 = [[UITextField alloc] initWithFrame:CGRectMake(25, 0, 335, 50)];
    self.register4.backgroundColor = [UIColor whiteColor];
    self.register4.placeholder = @"请输入邮箱";
    self.register4.font = [UIFont systemFontOfSize:20];
    self.register4.delegate = self;
    [sixthView addSubview:self.register4];
    
    UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeSystem];
    registerButton.frame = CGRectMake(15, 255, 345, 45);
    [registerButton setBackgroundImage:[UIImage imageNamed:@"rigis.png"] forState:UIControlStateNormal];
    [registerButton addTarget:self action:@selector(registerButtonAct:) forControlEvents:UIControlEventTouchUpInside];
    [self.registerView addSubview:registerButton];
    
    UIButton *otherRegisterButton = [UIButton buttonWithType:UIButtonTypeSystem];
    otherRegisterButton.frame = CGRectMake(260, 315, 95, 20);
    [otherRegisterButton setBackgroundColor:[UIColor clearColor]];
    [otherRegisterButton setTitle:@"更多注册方式" forState:UIControlStateNormal];
    [otherRegisterButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [otherRegisterButton addTarget:self action:@selector(otherRegisterButtonAct:) forControlEvents:UIControlEventTouchUpInside];
    [self.registerView addSubview:otherRegisterButton];
    
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 120 - 1, Width, 1)];
    imageView1.image = [UIImage imageNamed:@"spring_line@2x.png"];
    [self.registerView addSubview:imageView1];
    
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 70 - 1, Width, 1)];
    imageView2.image = [UIImage imageNamed:@"spring_line@2x.png"];
    [self.registerView addSubview:imageView2];
    
    UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 170 - 1, Width, 1)];
    imageView3.image = [UIImage imageNamed:@"spring_line@2x.png"];
    [self.registerView addSubview:imageView3];
    
    self.loginView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Width, Height - 64)];
    self.loginView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    [self.view addSubview:self.loginView];
    
    UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, Width, 50)];
    firstView.backgroundColor = [UIColor whiteColor];
    [self.loginView addSubview:firstView];
    
    self.firstField = [[UITextField alloc] initWithFrame:CGRectMake(25, 0, 335, 50)];
    self.firstField.backgroundColor = [UIColor whiteColor];
    self.firstField.placeholder = @"手机号/邮箱/用户名";
    self.firstField.font = [UIFont systemFontOfSize:20];
    self.firstField.delegate = self;
    [firstView addSubview:self.firstField];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 70 - 1, Width, 1)];
    imageView.image = [UIImage imageNamed:@"spring_line@2x.png"];
    [self.loginView addSubview:imageView];
    
    UIView *secondView = [[UIView alloc] initWithFrame:CGRectMake(0, 70, Width, 50)];
    secondView.backgroundColor = [UIColor whiteColor];
    [self.loginView addSubview:secondView];
    
    self.secondField = [[UITextField alloc] initWithFrame:CGRectMake(25, 0, 335, 50)];
    self.secondField.backgroundColor = [UIColor whiteColor];
    self.secondField.placeholder = @"密码";
    self.secondField.font = [UIFont systemFontOfSize:20];
    self.secondField.delegate = self;
    self.secondField.secureTextEntry = YES;
    self.secondField.clearButtonMode = UITextFieldViewModeAlways;
    [secondView addSubview:self.secondField];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAct:)];
    [self.view addGestureRecognizer:tap];
    
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
    loginButton.frame = CGRectMake(15, 155, 345, 45);
    [loginButton setBackgroundImage:[UIImage imageNamed:@"login@2x.png"] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(loginButtonAct:) forControlEvents:UIControlEventTouchUpInside];
    [self.loginView addSubview:loginButton];
    
    UIButton *loginButtonwei = [UIButton buttonWithType:UIButtonTypeSystem];
    loginButtonwei.frame = CGRectMake(60, 220, 110, 90);
    
    UIImageView *sinaImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 0, 70, 70)];
    sinaImageView.image = [UIImage imageNamed:@"binding_icon_sina@2x.png"];
    [loginButtonwei addSubview:sinaImageView];
    
    UILabel *sinaLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 70, 110, 20)];
    sinaLabel.text = @"新浪微博登陆";
    [loginButtonwei addSubview:sinaLabel];
    
    [loginButtonwei addTarget:self action:@selector(loginButtonAct:) forControlEvents:UIControlEventTouchUpInside];
    [self.loginView addSubview:loginButtonwei];
    
    UIButton *loginButtonqq = [UIButton buttonWithType:UIButtonTypeSystem];
    loginButtonqq.frame = CGRectMake(205, 220, 110, 90);
    
    UIImageView *qqImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 0, 70, 70)];
    qqImageView.image = [UIImage imageNamed:@"binding_icon_qq@2x.png"];
    [loginButtonqq addSubview:qqImageView];
    
    UILabel *qqLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 70, 110, 20)];
    qqLabel.text = @"QQ登录";
    qqLabel.textAlignment = NSTextAlignmentCenter;
    [loginButtonqq addSubview:qqLabel];
    
    [loginButtonqq addTarget:self action:@selector(loginButtonAct:) forControlEvents:UIControlEventTouchUpInside];
    [self.loginView addSubview:loginButtonqq];
    
}

- (void)tapAct:(UITapGestureRecognizer *)tap{
    [self.firstField resignFirstResponder];
    [self.secondField resignFirstResponder];
    [self.register1 resignFirstResponder];
    [self.register2 resignFirstResponder];
    [self.register3 resignFirstResponder];
    [self.register4 resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (void)leftBarButtonItemAction:(UIBarButtonItem *)barButtonItem{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

//分段按钮方法
-(void)segmentAction:(UISegmentedControl *)segment{
    if (segment.selectedSegmentIndex == 0) {
        [self.view bringSubviewToFront:self.loginView];
    }
    if(segment.selectedSegmentIndex == 1){
        [self.view bringSubviewToFront:self.registerView];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
}

#pragma mark - 登录逻辑按钮
- (void)loginButtonAct:(UIButton *)button{
    [self.firstField resignFirstResponder];
    [self.secondField resignFirstResponder];
    
    if ([self.firstField.text isEqualToString:@""] || [self.firstField.text isEqualToString:@""]) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入账户和密码" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];
    }
    else{
        [[DataBaseTools shareDataBase] openDB];
        NSMutableDictionary *dic = [[DataBaseTools shareDataBase] selectMessage];
        if (![dic.allKeys containsObject:self.firstField.text]) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"用户名不存在" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alertView show];
        }else{
            if (![[dic objectForKey:self.firstField.text] isEqualToString:self.secondField.text]) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"密码错误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                [alertView show];
            }
            else{
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"登陆成功" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                [alertView show];
                [[NSUserDefaults standardUserDefaults] setObject:self.firstField.text forKey:@"isLogin"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                [self dismissViewControllerAnimated:YES completion:^{
                    
                    
                }];
            }
        }
    }
}

#pragma mark - 其它方式注册逻辑
- (void)otherRegisterButtonAct:(UIButton *)button{
    NSLog(@"注册");
    YJViewController *yjVC = [[YJViewController alloc] init];
    [self.navigationController pushViewController:yjVC animated:YES];
}

#pragma mark - 注册按钮逻辑
- (void)registerButtonAct:(UIButton *)button{
    [self.register1 resignFirstResponder];
    [self.register2 resignFirstResponder];
    [self.register3 resignFirstResponder];
    [self.register4 resignFirstResponder];
    
    //判断是否写入注册信息，
    if ([self.register1.text isEqualToString:@""]) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入注册信息" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }
    else{
        if ([self.register2.text isEqualToString:@""]) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入密码" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
        }
        else{
            //判断两次输入密码是否一致
            if (![self.register2.text isEqualToString:self.register3.text]) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"两次输入密码不一致" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alertView show];
            }
            else{
                //如果一致
                //打开数据库
                [[DataBaseTools shareDataBase] openDB];
                //创建用户信息表
                [[DataBaseTools shareDataBase] createMessage];
                //得到用户信息，我这里返回的是字典 字典的key是用户名  value是密码
                NSMutableDictionary *dic = [[DataBaseTools shareDataBase] selectMessage];
                //判断所有用户名是否包含这个要注册的用户名
                if ([dic.allKeys containsObject:self.register1.text]) {
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"该用户名已经被注册" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    [alertView show];
                }
                else{
                    //如果没有相同的
                    //插入用户名，密码
                    [[DataBaseTools shareDataBase] insertMessageWithUserName:self.register1.text password:self.register2.text];
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"注册成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    [alertView show];
                    
                    self.segmentC.selectedSegmentIndex = 0;
                    [self.view bringSubviewToFront:self.loginView];
                    self.register1.text = nil;
                    self.register2.text = nil;
                    self.register3.text = nil;
                    self.register4.text = nil;
                }
            }
        }
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
